﻿using Abp.Authorization.Users;
using Abp.Collections.Extensions;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Extensions;
using Abp.Timing;
using Mt.Organizations;
using Mt.Site.Core.WorkFlows.Enum;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Mt.Site.Core.WorkFlows.Process.Entities
{
    // 修改后要同步修改 WorkFlowPaperArchive
    public class WorkFlowPaper : AuditedEntity<Guid>
    {
        public const int MaxTitleLength = 500;
        public const int MaxSignerLength = 64;
        public const int MaxReferNumberLength = 64;
        public const int CoOperativeUnitIdsLength = 1000;
        public const int MaxWhNameLength = 100;

        /// <summary>
        /// 所属流程
        /// </summary>
        public Guid WorkFlowId { get;protected set; }

        /// <summary>
        /// 当前所处步骤
        /// </summary>
        public Guid NodeId { get; protected set; }

        public PaperState PaperState { get; protected set; }


        [StringLength(MaxTitleLength)]
        public string Title { get; protected set; }

        //部门名（来源）
        [StringLength(MtOrganizationUnit.MaxShortNameLength)]
        public string UnitName { get; protected set; }

        //确定发文部门的值（主办部门）
        public long UnitId { get; protected set; }

        /// <summary>
        /// 协办部门id
        /// </summary>
        [StringLength(CoOperativeUnitIdsLength)]
        public string CoOperativeUnitIds { get; protected set; }


        [StringLength(CoOperativeUnitIdsLength)]
        public string MainOperativeUserId { get; protected set; }

        [StringLength(CoOperativeUnitIdsLength)]
        public string CoOperativeUserIds { get; protected set; }

        [StringLength(CoOperativeUnitIdsLength)]
        public string CoOperativeUserIdsCopy { get; protected set; }

        [StringLength(AbpUserBase.MaxNameLength)]
        public string UserName { get; protected set; }

        /// <summary>
        /// 流水号
        /// </summary>
        public long SerialNumber { get; protected set; }

        /// <summary>
        /// 签发人（未使用）
        /// </summary>
        [StringLength(MaxSignerLength)]
        public string Signer { get; protected set; }

        /// <summary>
        /// 文号
        /// </summary>

        [Required]
        [StringLength(MaxReferNumberLength)]
        public string ReferNumber { get; protected set; }

        /// <summary>
        /// 分发数量（备用）
        /// </summary>
        public int PrintCount { get; protected set; }

        public ApprovalResult ApprovalResult { get; protected set; }

        public ProgressState ProgressState { get; protected set; }

        public bool HasFinished { get; protected set; }

        /// <summary>
        /// 办理期限
        /// </summary>
        public DateTime? DeadLineTime { get; protected set; }

        /// <summary>
        /// 登记时间
        /// </summary>
        public DateTime RegistrationTime { get; protected set; }

        public int WhYear { get; protected set; }

        public int WhNumber { get;protected set; }

        [StringLength(MaxWhNameLength)]
        public string WhName { get;protected set; }

        public Guid? WenHaoId { get; protected set; }

        protected WorkFlowPaper()
        {

        }

        public WorkFlowPaper(string title, Guid workflowId, Guid nodeId, string userName, long serialNumber)
        {
            WorkFlowId = workflowId;
            NodeId = nodeId;
            Title = title;
            SerialNumber = serialNumber;
            UserName = userName;

            PaperState = PaperState.Temporary;   //临时稿件
            Id = Guid.NewGuid();

            ReferNumber = "";
        }
        public void ChangeUnit(long unitId,string unitName)
        {
            UnitId = unitId;
            UnitName = unitName;
        }
        public void ChangePaperState(PaperState state)
        {
            PaperState = state;
        }
        public void ChangeProgressState(ProgressState state)
        {
            ProgressState = state;
        }
        public void ChangeApprovalResult(ApprovalResult approvalResult)
        {
            ApprovalResult = approvalResult;
        }
        public void SetFinishedState(bool hasFinished)
        {
            HasFinished = hasFinished;
        }

        public void ChangeTitle(string title)
        {
            Title = title;
        }

        public void ChangeRegistrationTime(string registrationTime)
        {
            if(!registrationTime.IsNullOrEmpty())
            {
                try
                {
                    RegistrationTime = long.Parse(registrationTime).FromUnixTimestampByMilliseconds();
                }
                catch
                {
                    RegistrationTime = CreationTime;
                }
            }
            else
            {
                RegistrationTime = CreationTime;
            }
        }

        public void ChangeDeadlineTime(string deadlineTime)
        {
            if (!deadlineTime.IsNullOrEmpty())
            {
                try
                {
                    DeadLineTime = long.Parse(deadlineTime).FromUnixTimestampByMilliseconds();
                }
                catch
                {
                    DeadLineTime = null;
                }
            }
            else
            {
                DeadLineTime = null;
            }
        }

        public void ChangeReferNumber(string referNumber)
        {
            ReferNumber = referNumber;
        }

        public void ChanageReferInfo(int whYear,int whNumber,string whName)
        {
            WhYear = whYear;
            WhNumber = whNumber;
            WhName = whName;
        }

        public void ChangeNode(Guid nodeId)
        {
            NodeId = nodeId;
        }

        //public void SetOperativeUser(string mainOperativeUserId, string coOperativeUserIds)
        //{
        //    var coOperativeUserIdList = coOperativeUserIds.GetArryFromStrBySplit<long>(WorkFlowConst.SplitChars);
        //    var mainOperativeUserIdList = mainOperativeUserId.GetArryFromStrBySplit<long>(WorkFlowConst.SplitChars);

        //    MainOperativeUserId = mainOperativeUserIdList.JoinAsString(WorkFlowConst.SplitChars);
        //    CoOperativeUserIds = coOperativeUserIdList.Distinct().Where(p => !mainOperativeUserIdList.Contains(p)).JoinAsString(WorkFlowConst.SplitChars); ;
        //    CoOperativeUserIdsCopy = CoOperativeUserIds;
        //}

        //public void SetMainOperativeUnit(string unitName, long unitId)
        //{
        //    UnitName = unitName;
        //    UnitId = unitId;
        //}
        //public void UpdateCoOperativeUser(string coOperativeUserIdsCopy)
        //{
        //    CoOperativeUserIdsCopy = coOperativeUserIdsCopy;
        //}

        //public void SetWenHaoData(int whYear,int whNumber,Guid? wenHaoId,string referenceNumber,string whName) {
        //    WhYear = whYear;
        //    WhNumber = whNumber;
        //    WenHaoId = wenHaoId;
        //    ReferNumber = referenceNumber;
        //    WhName = whName;
        //}

        public void SetSigner(string signer)
        {
            Signer = signer;
        }
    }
}
